/* gtkmm.cc : GTKMM interface */

#include <gtkmm/calendar.h>
#include <gtkmm/main.h>

#include "gtkmm.hh"

#include "dialogs_provider.hh"
#include "main_interface.hh"

#ifdef HAVE_X11_XLIB_H
#include <X11/Xlib.h>
#endif

#include "../../../share/vlc32x32.xpm"
#include "../../../share/vlc32x32-christmas.xpm"

static int  Open        (vlc_object_t*);
static void Close       (vlc_object_t*);
static int  OpenDialogs (vlc_object_t*);
static void Run         (intf_thread_t*);
static void Init        (intf_thread_t*);
static void ShowDialog  (intf_thread_t*, int, int, intf_dialog_args_t*);

vlc_module_begin ();

  set_shortname ((char *)"Gtkmm");
  set_description ((char *)_("Gtkmm interface"));
  set_capability ("interface", 151);
  set_category (CAT_INTERFACE);
  set_subcategory (SUBCAT_INTERFACE_MAIN);
  set_callbacks (Open, Close);

  add_shortcut ("gtkmm");
    
  add_submodule ();
    set_description (_("Gtkmm dialogs provider"));
    set_capability ("dialogs provider", 51);
    set_callbacks (OpenDialogs, Close);
    
vlc_module_end ();

static int
Open (vlc_object_t *p_this)
{

  intf_thread_t *p_intf = (intf_thread_t*)p_this;
  p_intf->pf_run = Run;

#if defined HAVE_X11_XLIB_H
  Display *display = XOpenDisplay (NULL);

  if (!display)
   {
    msg_Err (p_intf, "Could not connect to X server");
    return VLC_EGENERIC;
   }

  XCloseDisplay (display);
#endif

  p_intf->p_sys = (intf_sys_t*) malloc (sizeof (intf_sys_t));

  if (!p_intf->p_sys)
   {
    msg_Err (p_intf, "Out of memory");
    return VLC_ENOMEM;
   }

  memset (p_intf->p_sys, 0, sizeof (intf_sys_t));

  p_intf->p_sys->p_playlist = pl_Yield (p_intf);
  p_intf->p_sys->p_sub = msg_Subscribe (p_intf, MSG_QUEUE_NORMAL);

  p_intf->b_play = true;

  return VLC_SUCCESS;

}

static int
OpenDialogs (vlc_object_t *p_this)
{

  intf_thread_t *p_intf = (intf_thread_t*)p_this;
  int val = Open (p_this);

  if (val)
    return val;

  p_intf->pf_show_dialog = ShowDialog;
  
  return VLC_SUCCESS;

}

static void
Close (vlc_object_t *p_this)
{

  intf_thread_t *p_intf = (intf_thread_t*)p_this;

  vlc_mutex_lock (&p_intf->object_lock);
    p_intf->b_dead = true;
  vlc_mutex_unlock (&p_intf->object_lock);

  vlc_object_release (p_intf->p_sys->p_playlist);
  msg_Unsubscribe (p_intf, p_intf->p_sys->p_sub); 
  free (p_intf->p_sys);

}

/*****************************************************************************
 * Initialize the interface or the dialogs provider
 *****************************************************************************/
static void
Run (intf_thread_t *p_intf)
{

  if (p_intf->pf_show_dialog)
   {
    if (vlc_thread_create (p_intf, "Gtkmm dialogs", Init, 0, true))
      msg_Err (p_intf, "failed to create Gtkmm dialogs thread");
   }
  else
    Init (p_intf);

}

static void
Init (intf_thread_t *p_intf)
{

  int argc = 1;
  char *args[] = {(char*)"", NULL};
  char **argv  = args;

  Gtk::Main kit (argc, argv);

  Glib::Date date;
  Gtk::Calendar ().get_date (date);

  if (date.get_day_of_year () >= 354)
    Gtk::Window::set_default_icon (Gdk::Pixbuf::create_from_xpm_data (vlc_christmas_xpm));
  else
     Gtk::Window::set_default_icon (Gdk::Pixbuf::create_from_xpm_data (vlc_xpm));

  if (!p_intf->pf_show_dialog)
   {
    MainInterface *mi = new MainInterface (p_intf);
    p_intf->p_sys->p_mi = mi;
    mi->show_all ();
   }
  else
    vlc_thread_ready (p_intf);

  if (!p_intf->pf_show_dialog && p_intf->b_play)
   {
    playlist_Control (THEPL, PLAYLIST_PLAY, false);
   }

  p_intf->pf_show_dialog = ShowDialog;

  kit.run ();

  if (p_intf->p_sys->p_mi)
    delete p_intf->p_sys->p_mi;
  
}

/*****************************************************************************
 * Callback to show a dialog
 *****************************************************************************/
static void
ShowDialog (intf_thread_t *p_intf, int i_dialog_event, int i_arg, intf_dialog_args_t *p_arg)
{}

